<template>
	<view class="order-item bg-white radius-16 padding-sm margin-bottom-sm">
		<!-- 订单头部-订单编号&订单状态-开始 -->
		<view class="flex align-center justify-between text-666 margin-bottom">
			<view class="time">
			  <view class=" text-df text-333 flex algin-center"  style="align-items: center;" v-if='item.shop_info' @click.stop="goMerchant(item.shop_info)">
				 <!-- <view class="kj-tag" v-if="item.is_goods_type == 1">抢购</view>
				  <view class="czg-tag" v-if="item.is_goods_type == 2">超值购</view> -->
			    <image class="merchant-icon" :src="item.shop_info.store_logo"  mode="aspectFill" />
			    <text>{{item.shop_info.store_name}} </text>
			    <text class="fu-iconfont merchant-icon-left">&#xe80e;</text>
			  </view>
			</view>
			<!-- 非预售订单状态判断 start -->
			<view v-if="item.order_type!=7">
				<text v-if="item.send_type != 1" class="text-theme">{{ item.status | filterStatus }}</text>
				<text v-else class="text-theme">{{ item.status | filterInviteStatus }}</text>
			</view>
			<!-- 非预售订单状态判断 end -->
			<!-- 预售订单状态判断 start -->
			<view v-if="item.order_type==7">
				<text v-if="item.final_order.pay_status==0 && item.status==7" class="text-theme">待付尾款</text>
				<text v-if="item.final_order.pay_status==0 && item.status==0" class="text-theme">待付定金</text>
				<text v-if="item.final_order.pay_status==0 && item.status<0" class="text-theme">已取消</text>
				<text v-if="item.send_type != 1 && item.final_order.pay_status==1"
					class="text-theme">{{ item.status | filterStatus }}</text>
				<text v-if="item.send_type == 1 && item.final_order.pay_status==1"
					class="text-theme">{{ item.status | filterInviteStatus }}</text>
			</view>
			<!-- 预售订单状态判断 end -->
		</view>
		<!-- 订单头部-订单编号&订单状态-结束 -->
		<!-- 订单商品开始 -->
		<view class="solid-bottom" @tap.stop="navToOrderDetail(item, index)">
			<block v-for="(goodsItem, goodsIndex) in item.goods" :key="goodsIndex">
				<view class="flex align-stretch padding-bottom">
					<view class="fu-block-160 margin-right-sm">
						<fu-image :src="goodsItem.goods_thumb" radius="8" mode="aspectFill"></fu-image>
					</view>
					<view class="flex-sub flex flex-direction justify-between" style="min-width: 1px;">
						<view>
							<view class="text-cut-2 text-333 text-bold fu-fs28">{{ goodsItem.goods_name }}</view>
							<view class="fu-fs24 text-999 text-cut margin-top-xs">{{ goodsItem.sku_name }}</view>
						</view>
						<!-- <view class="text-right text-orange text-sm" v-if="goodsItem.order_refund_status == -2">
							{{ i18n['售后取消'] }}
						</view>
						<view class="text-right text-orange text-sm" v-if="goodsItem.order_refund_status == -1">
							{{ i18n['申请驳回'] }}
						</view>
						<view class="text-right text-orange text-sm" v-if="goodsItem.order_refund_status == 0">
							{{ i18n['售后中'] }}
						</view>
						<view class="text-right text-orange text-sm" v-if="goodsItem.order_refund_status == 1">
							{{ i18n['同意申请'] }}
						</view>
						<view class="text-right text-orange text-sm" v-if="goodsItem.order_refund_status == 2">
							{{ i18n['商家确认收货'] }}
						</view>
						<view class="text-right text-orange text-sm" v-if="goodsItem.order_refund_status == 3">
							{{ i18n['商家收货'] }}
						</view>
						<view class="text-right text-orange text-sm" v-if="goodsItem.order_refund_status == 4">
							{{ i18n['退款成功'] }}
						</view> -->
						<!-- v-if="goodsItem.order_refund_status == -999" -->
						<!-- <view class="text-right text-orange text-sm" v-if="item.order_type == 3 || item.order_type == 4">
							{{ i18n['申请售后'] }}
						</view> -->
						<!-- 待评价显示赠送喵币 -->
					<!-- 	<view v-if="mewNum > 0 && item.status == 3" style="font-size: 24rpx;">
							<text>评论可获得</text>
							<text style="color:#ff6f27;font-weight: 800;padding:0 6rpx">{{mewNum}}</text>
							<text>喵币</text>
						</view> -->
						<view class="flex justify-between align-center">
							<text class="text-333 text-bold" v-if="item.order_type=='10'">
								<text class="text-price fu-fs20"></text>
								<text class="fu-fs32">{{ goodsItem.activity_price | filterPriceInt}}.</text>
								<text class="fu-fs22">{{ goodsItem.activity_price | filterPriceFloat}}</text>
								<text  class="text-through text-999 margin-left text-price">{{goodsItem.shop_price}}</text>
							</text>
							<text class="text-bold text-333" v-else>
								<text class=" fu-fs32">￥</text>
								<text class="fu-fs32">{{ goodsItem.shop_price | filterPriceInt}}.</text>
								<text class="fu-fs22">{{ goodsItem.shop_price | filterPriceFloat}}</text>
							</text>
							<text class="fu-fs24 text-999">x {{ goodsItem.num }}</text>
						</view>
					</view>
				</view>
			</block>
		</view>
		<!-- 订单商品结束 -->
		<!-- 操作按钮开始 -->
		<view class="padding-top relative">
			<view class="flex">
				<view style="width:50px;" v-if="isContentShow([5, 6, 7,10], item.order_type)">
					<!-- order_type 3普通订单 4积分订单 5拼团订单 6秒杀订单 7预售订单 -->
					<text class="cu-tag round line-red">{{item.order_type|orderTypeName}}</text>
				</view>
				<view class="flex-sub">
					<view class="flex align-center justify-end">
						<text class="fu-fs24 text-666">{{i18n['共']}}</text>
						<text class="fu-fs24 text-666">{{num}}</text>
						<text class="fu-fs24 text-666">{{i18n['件商品']}}</text>
						<text class="fu-fs24 margin-lr-xs text-666" v-if="item.status == 0 || ((item.status == -1||item.status == -2) && item.pay_time == 0)">{{i18n['应付款']}}</text>
						<text class="fu-fs24 margin-lr-xs text-666" v-else>{{i18n['实付款']}}</text>
						<text class="text-bold" style="color: #FF5C5C;">
							<text class="text-price fu-fs20"></text>
							<text class="fu-fs32">{{ item.payable_money | filterPriceInt}}.</text>
							<text class="fu-fs22">{{ item.payable_money | filterPriceFloat}}</text>
						</text>
					</view>
				</view>
			</view>
			<view class="flex align-center justify-end margin-top" style="align-items: center;" v-if="isContentShow([3,4,5,6, 10], item.order_type)">
				<view
					class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-f6 text-333"
					v-if="isContentShow([0], item.status)" @tap.stop="showCancelReason">
					{{ i18n['取消订单'] }}
				</view>
				<view style="background-color: #FD9CAE;"
					class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-theme text-white"
					v-if="isContentShow([0], item.status)" @tap.stop="toPay">
					{{ i18n['去付款'] }}
				</view>
				<!-- <view
					class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-f6 text-333"
					v-if="isContentShow([1, 2, 3, 4], item.status) && item.real_money > 0" @tap.stop="handleInvoice">
					<text v-if="item.invoice_status == 0">申请开票</text>
					<text v-if="item.invoice_status == 1 || item.invoice_status == 2">查看发票</text>
				</view>
				<view
					class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-f6 text-333 btn"
					v-if="isContentShow([-2, -1, 4, 6, 7], item.status)" @tap.stop="delOrder" style="color: #333333;">
					{{ i18n['删除订单'] }}
				</view>
				<view
					class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-f6 text-333"
					v-if="isContentShow([2], item.status) && item.send_type != 1" @tap.stop="seelogs">
					{{ i18n['查看物流'] }}
				</view>
				<view
					class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-f6 text-333 btn"
					v-if="isContentShow([1, 2, 3, 4], item.status) && item.stop_refund == 0"
					@tap.stop="navToOrderDetail(item, index)">
					{{ i18n['申请售后'] }}
				</view>
				<view
					class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-theme text-white btn"
					v-if="isContentShow([1], item.status) && item.is_remind.info == 0" @tap.stop="remindOrder">
					{{ i18n['提醒发货'] }}
				</view>
				<view
					class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-theme text-white btn"
					style="opacity: 0.7;" v-if="isContentShow([1], item.status) && item.is_remind.info == 1">
					{{ i18n['已提醒'] }}
				</view> -->
				<!-- <view
					class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-theme text-white btn"
					v-if="item.group_info_is_full == 0 && item.order_type == 5" :style="{'opacity': '0.7'}">
					{{ i18n['确认收货'] }}
				</view> -->
				<!-- <template v-if="item.group_info_is_full == 0 && item.order_type != 5 || item.group_info_is_full == 1 && item.order_type == 5 || item.order_type != 5">
					<view
						class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-theme text-white btn"
						v-if="isContentShow([2], item.status)" @tap.stop="receiveOrder()">
						{{ i18n['确认收货'] }}
					</view>
				</template> -->
				<view style="background-color: #FD9CAE;"
					class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-theme text-white btn"
					v-if="isContentShow([3], item.status)" @tap.stop="evaluateOrder">
					去评价
				</view>
				<view style="background-color: #FD9CAE;"
					class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-theme text-white btn"
					v-if="isContentShow([6], item.status)" @tap.stop="navToOrderDetail(item, index)">
					查看详情
				</view>
				<view style="background-color: #FD9CAE;"
				class="round width-170 height-60 fu-fs24 margin-right-sm text-center line-height-60 bg-theme text-white btn" v-if="isContentShow([1], item.status)" @click="hexiao">核销码</view>
			</view>
		</view>
		<!-- 操作按钮结束 -->
	</view>
</template>

<script>
	export default {
		props: {
			item: {
				type: Object,
				default () {
					return {};
				}
			},
			index: {
				type: Number,
				default: 0
			},
			mewNum:{
				type:[Number,String],
				default: 0
			}
		},
		data() {
			return {}
		},
		computed: {
			num() {
				let num = 0;
				if (this.item.goods && this.item.goods.length > 0) {
					this.item.goods.forEach(item => {
						num += item.num;
					})
				}
				return num;
			}
		},
		methods: {
			goMerchant(item) {
			  if (item.status == '0') return this.$message.info("该店铺已关闭");
			  this.$urouter.navigateTo({
			    url: '/pages/goods/shop/shop/index',
			    params: {
			      partner_id: item.u_id
			    }
			  })
			},
			/**
			 * @description 取消订单
			 * @param {String} item = 订单列表项
			 * @param {Number} index = 当前订单在列表中的索引
			 * */
			showCancelReason() {
				this.$emit('showCancelReason', {
					item: this.item,
					index: this.index
				});
			},
			/**
			 * @description 去支付
			 * @param {string} sn = 订单号
			 * @param {Number} type = 订单类型
			 * */
			toPay() {
				this.$urouter.navigateTo({
					url: '/pages/order/order-process/pay-order/index',
					params: {
						order_sn: this.item.order_sn,
						order_type: this.item.order_type,
						itemIndex: this.index
					}
				});
			},
			/**
			 * @description 去支付尾款
			 * @param {string} sn = 订单号
			 * @param {Number} type = 订单类型
			 * */
			toPayFinal(item) {
				if(item.can_pay==0) return;
				this.$urouter.navigateTo({
					url: '/pages/order/order-process/pay-order/index',
					params: {
						order_sn: item.order_sn,
						order_type: this.item.order_type,
						itemIndex: this.index
					}
				});
			},
			/**
			 * @description 删除订单
			 * @param {Object} item = 当前订单项对象
			 * @param {Number} index = 当前订单在列表中的索引
			 */
			delOrder() {
				this.$util.showModal({
					title: this.i18n['确认删除此订单?'],
					content: this.i18n['删除后不可恢复'],
					success: res => {
						if (res.confirm) {
							this.$api.post(global.apiUrls.postDelOrder, {
								order_sn: this.item.order_sn
							}).then(result => {
								if (result.data.code == 1) {
									this.$message.info(this.i18n['删除成功']);
									uni.$emit('onPageUpdata', {
										type: 4, // 1: 取消订单， 2: 提醒发货， 3: 确认订单， 4: 删除订单
										index: this.index,
									})
								} else {
									this.$message.info(data.msg);
								}
							}).catch(err => {
								this.$message.info('服务器错误，请稍后重试');
							});
						}
					}
				});
			},
			// 提醒发货
			remindOrder() {
				this.$api.post(global.apiUrls.postRemindShipment, {
					order_sn: this.item.order_sn
				}).then(res => {
					if (res.data.code == 1) {
						this.$message.info(this.i18n['已提醒,请耐心等待']);
						uni.$emit('onPageUpdata', {
							type: 2, // 1: 取消订单， 2: 提醒发货， 3: 确认订单， 4: 删除订单
							index: this.index,
						})
					} else {
						this.$message.info(res.data.msg);
					}
				}).catch(err => {
					this.$message.info('服务器错误，请稍后重试');
				});
			},
			// 核销码
			hexiao() {
				this.$emit('hexiao')
			},
			/**
			 * @description 确认收货
			 * @param {Object} item = 当前订单项对象
			 * @param {Number} index = 当前订单在列表中的索引
			 * */
			receiveOrder() {
				this.$util.showModal({
					title: this.i18n['提示'],
					content: this.i18n['您是否确认收到货物?'],
					success: res => {
						if (res.confirm) {
							this.$api.post(global.apiUrls.postConfirmOrder, {
								order_sn: this.item.order_sn
							}).then(res => {
								this.$message.info(res.data.msg);
								if (res.data.code == 1) {
									uni.$emit('onPageUpdata', {
										type: 3, // 1: 取消订单， 2: 提醒发货， 3: 确认收货， 4: 删除订单
										index: this.index,
									})
								} else {

								}
							}).catch(err => {
								this.$message.info('服务器错误，请稍后重试');
							});
						}
					}
				});
			},

			seelogs() {
				this.$urouter.navigateTo({
					url: '/pages/order/orderdetail/express-delivery/index',
					params: {
						order_sn: this.item.order_sn
					}
				});
			},
			/**
			 * @description 查看订单详情
			 * @param {String} item = 订单列表项
			 * @param {Number} index = 当前订单在列表中的索引
			 * */
			navToOrderDetail(item, index) {
				this.$urouter.navigateTo({
					url: '/pages/order/orderdetail/order-detail/index',
					params: {
						order_sn: item.order_sn,
						order_type: item.order_type,
						itemIndex: index
					}
				});
			},
			/**
			 * @description 去评价
			 * @param {string} item = 订单列表项
			 * */
			evaluateOrder() {
				console.log(this.item);
				this.item.goods[0].business_hours = this.item.shop_info.business_hours
				console.log(this.item.goods[0].business_hours );
				// this.item.goods
				uni.setStorageSync('order_goods_list', JSON.stringify(this.item.goods));
				this.$urouter.navigateTo({
					url: '/pages/order/evaluation/order-evaluation/index',
					params: {
						order_sn: this.item.order_sn,
						order_type: this.item.order_type,
						itemIndex: this.index
					}
				});
			},
			// 发票
			handleInvoice() {
				// status == 0去申请开票
				if (this.item.invoice_status == 0) {
					this.$urouter.navigateTo({
						url: '/pages/invoice/invoice/apply-invoice/index',
						params: {
							status: this.item.invoice_status,
							order_sn: this.item.order_sn,
							payable_money: this.item.payable_money,
							itemIndex: this.index
						}
					})
				}
				// status == 1已申请过发票  可以修改发票（修改，撤销发票）   只有查看了发票才知道是否需要修改
				//  status == 2发票已经开票成功  不能再修改
				if (this.item.invoice_status == 1 || this.item.invoice_status == 2) {
					this.$urouter.navigateTo({
						url: '/pages/invoice/invoice/invoice-info/index',
						params: {
							order_sn: this.item.order_sn,
							payable_money: this.item.payable_money
						}
					})
				}
			},
			/**
			 * @description 判断内容是否显示
			 * @param {String} arr = 要判端数组
			 * @param {String | Number} val = 要判的值
			 */
			isContentShow(arr, val) {
				return arr.includes(val * 1);
			},
		},
		filters: {
			// 订单状态过滤器  status每个值代表的状态
			filterStatus(val) {
				switch (val) {
					case 0:
						return '待付款';
					case 1:
						return '待使用';
					case 2:
						return global.i18n['待收货'];
					case 3:
						return global.i18n['待评价'];
					case 4:
						return '已完成';
					case 5:
						return global.i18n['售后中'];
					case 6:
						return global.i18n['售后已完成'];
					case -1:
						return '已取消';
					case -2:
						return '交易关闭';
					case -4:
						return '已过期';
					default:
						return '';
				}
			},
			// 自提状态过滤器
			filterInviteStatus(val) {
				switch (val) {
					case 0:
						return '待付款';
					case 1:
						return '待取货';
					case 2:
						return '待取货';
					case 3:
						return '提货成功';
					case 4:
						return '已完成';
					case 6:
						return '售后中';
					case 7:
						return '售后已完成';
					case -1:
						return '交易关闭';
					case -2:
						return '交易关闭';
					default:
						return '';
				}
			},
			filterPriceInt(val) {
				if (!val) return "";
				return val.toString().split('.')[0];
			},
			filterPriceFloat(val) {
				if (!val) return "";
				return val.toString().split('.')[1];
			},
		}
	}
</script>

<style scoped lang="scss">
	.order-item {
		.btn {
			&:last-child {
				margin-right: 0;
			}
		}
	}
	.merchant-icon {
	  width: 40rpx;
	  height: 40rpx;
	  margin-left: 0rpx;
	  margin-right: 16rpx;
	  top: 2rpx;
	}
	
	.merchant-icon-left {
	  margin-left: 16rpx;
	  font-size: 24rpx;
	}
	.kj-tag {
		padding: 4rpx 8rpx;
		border-radius: 8rpx;
		background-color: #FD9CAE;
		color: #fff;
		font-size: 24rpx;
		margin-right: 24rpx;
	}
	.czg-tag {
		padding: 4rpx 8rpx;
		border-radius: 8rpx;
		background-color:#FD9CAE,;
		color: #fff;
		font-size: 24rpx;
		margin-right: 24rpx;
	}
</style>
